<?php
class M_Oauth
{
  static public function auth_qq($code)
  {
header("Content-type: text/html; charset=utf-8"); 
    $app_id = OAUTH_QQ_APP_KEY;
    $app_secret = OAUTH_QQ_SECRET_KEY;
    //$code = $_REQUEST["code"];

    //Step2 get access token
    $token_url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&"
      . "client_id=" . $app_id . "&redirect_uri=" . OAUTH_QQ_REDIRECT_URI
      . "&client_secret=" . $app_secret . "&code=" . $code;

    $response = file_get_contents($token_url);
    M_Tools::log($response,1);
    if (strpos($response, "callback") !== false)
      {
	$lpos = strpos($response, "(");
	$rpos = strrpos($response, ")");
	$response  = substr($response, $lpos + 1, $rpos - $lpos -1);
	$msg = json_decode($response);
	if (isset($msg->error))
	  {
	    echo "<h3>error:</h3>" . $msg->error;
	    echo "<h3>msg  :</h3>" . $msg->error_description;
	    exit;
	  }
      }

    //Step3 get openid
    $params = array();
    parse_str($response, $params);

    $graph_url = "https://graph.qq.com/oauth2.0/me?access_token="
      .$params['access_token'];

    M_Tools::log($graph_url, 1);
    $str  = file_get_contents($graph_url);
    if (strpos($str, "callback") !== false)
      {
	$lpos = strpos($str, "(");
	$rpos = strrpos($str, ")");
	$str  = substr($str, $lpos + 1, $rpos - $lpos -1);
      }
    $info = json_decode($str);
    if (isset($info->error))
      {
	echo "<h3>error:</h3>" . $info->error;
	echo "<h3>msg  :</h3>" . $info->error_description;
	exit;
      }
    $openid = $info->openid;
    M_Tools::log($openid, 1);

    $tbl = new M_qin_users();
    $user = $tbl->get_user_with_openid('qq', $openid);
    if (!$user || !isset($user['id']))
      {
	M_Tools::log("not found user in db", 1);
	$url = "https://graph.qq.com/user/get_user_info?access_token=".$params['access_token']."&oauth_consumer_key=".$app_id."&openid=".$openid;
	M_Tools::log("get user info", 1);
	M_Tools::log($url, 1);
	$res = file_get_contents($url);
	$user_info = json_decode($res);
	M_Tools::print_r($user_info, 1);
	M_Tools::log("insert into table", 1);
	$data = array(
			"first_name"=>mb_substr($user_info->nickname,0,1,'UTF-8'),
			"name"=>mb_substr($user_info->nickname,1,16,'UTF-8'),
			"gender" => $user_info->gender=='男' ? MALE : FEMALE,
			"figureurl"=>$user_info->figureurl_2,
			"openid"=>$openid,
			"oauth"=>"qq"
		     );
	M_Tools::print_r($data, 1);
	// record this user
	$user = $tbl->add(array("first_name"=>mb_substr($user_info->nickname,0,1,'UTF-8'),
				"name"=>mb_substr($user_info->nickname,1,16,'UTF-8'),
				"gender" => $user_info->gender=='男' ? MALE : FEMALE,
				"figureurl"=>$user_info->figureurl_2,
				"openid"=>$openid,
				"oauth"=>"qq"
				));
	M_Tools::print_r($user, 1);
      }
    return $user;
  }
  static public function auth_weibo($code)
  {
    $res = file_get_contents("http://".DOMAIN."/index/get_access_token_from_sina_weibo?code=".$code);
    $r = json_decode($res);
    M_Tools::print_r($res, 1);
    if ($r->access_token)
      {
	M_Tools::log("get from db", 1);
	$tbl = new M_qin_users();
	$user = $tbl->get_user_with_openid('weibo', $r->uid);
	M_Tools::print_r($user, 1);
	if (!$user || !isset($user['id']))
	  {

		  M_Tools::log("not found from db", 1);
	    // get user info
	    M_Tools::log("get user info from: ", 1);
	    $url = "https://api.weibo.com/2/users/show.json?access_token=".$r->access_token."&uid=".$r->uid;
	    M_Tools::log($url, 1);
	    $res = file_get_contents($url);
	    $user_info = json_decode($res);
	    M_Tools::print_r($user_info, 1);

	    // return if error occurs
	    if ($user_info->error_code)
		    return array('error'=>$user_info->error_code, "message"=>$user_info->error);

	    $data = array("first_name"=>mb_substr($user_info->name,0,1,'UTF-8'),
				    "name"=>mb_substr($user_info->name,1,16,'UTF-8'),
				    "gender" => $user_info->gender=='m' ? MALE : FEMALE,
				    "figureurl"=>$user_info->avatar_large,
				    "openid"=>$r->uid,
				    "oauth"=>"weibo"
				    );
	    M_Tools::log("insert data", 1);
	    M_Tools::print_r($data, 1);
	
	    $user = $tbl->add(array("first_name"=>mb_substr($user_info->name,0,1,'UTF-8'),
				    "name"=>mb_substr($user_info->name,1,16,'UTF-8'),
				    "gender" => $user_info->gender=='m' ? MALE : FEMALE,
				    "figureurl"=>$user_info->avatar_large,
				    "openid"=>$r->uid,
				    "oauth"=>"weibo"
				    ));

	  }
	return $user;
      }
    else
      {
	return array('error'=>100, "message"=>"Get access token failed. ");
      }

  }

  static public function auth_kaixin($code)
  {
    $url = "https://api.kaixin001.com/oauth2/access_token?grant_type=authorization_code&code=".$code."&client_id=".OAUTH_KAIXIN_APP_KEY."&client_secret=".OAUTH_KAIXIN_SECRET_KEY."&redirect_uri=".OAUTH_KAIXIN_REDIRECT_URI;
    $str = file_get_contents($url);
    $r = json_decode($str);
    if (!$r->access_token)
	return NULL;

    $url = "https://api.kaixin001.com/users/me.json?access_token=".$r->access_token;
    $str = file_get_contents($url);
    $user_info = json_decode($str);

    $tbl = new M_qin_users();
    $user = $tbl->get_user_with_openid('kaixin', $user_info->uid);
	
    if (!$user || !isset($user['id']))
      {
	$user = $tbl->add(array("first_name"=>mb_substr($user_info->name,0,1,'UTF-8'),
				"name"=>mb_substr($user_info->name,1,16,'UTF-8'),
				"gender" => $user_info->gender ? MALE : FEMALE,
				"figureurl"=>$user_info->logo50,
				"openid"=>$user_info->uid,
				"oauth"=>"kaixin"
				));

      }
    return $user;
  }

  static public function auth_renren($code)
  {
    $url = "https://graph.renren.com/oauth/token?grant_type=authorization_code&client_id=".OAUTH_RENREN_APP_KEY."&redirect_uri=".OAUTH_RENREN_REDIRECT_URI."&client_secret=".OAUTH_RENREN_SECRET_KEY."&code=".$code;
    $str = file_get_contents($url);
    $r = json_decode($str);
    $user_info = $r->user;

    $tbl = new M_qin_users();
    $user = $tbl->get_user_with_openid('renren', $user_info->id);
    if (!$user || !isset($user['id']))
      {
	$user = $tbl->add(array("first_name"=>mb_substr($user_info->name,0,1,'UTF-8'),
				"name"=>mb_substr($user_info->name,1,16,'UTF-8'),
				/* "gender" => $user_info->gender, */
				"figureurl"=>$user_info->avatar[0]->url,
				"openid"=>$user_info->id,
				"oauth"=>"renren"
				));
	
      }
    return $user;
  }

}
